import requests
from bs4 import BeautifulSoup


def getHtml(url):
    try:
        re = requests.get(url, timeout=10)
        re.raise_for_status()
        re.encoding = 'utf-8'
        print('获取成功')
        return re.text
    except:
        print('获取失败')
        return ''


def getHtmlData(html):
    soup = BeautifulSoup(html, 'html.parser')
    ls_div = soup.find_all('div', {'class': 'page_list'})
    ls_ul = ls_div[0].find_all('ul')
    # print(ls_ul)
    ls_all = []
    for ul in ls_ul:
        ls = []
        if len(ul)==5:
            li_time=ul.find_all('li',{'class':'page_list_date'})
            ls.append(li_time[0].string)
            ls.append(ul.find('a').attrs['title'])
            ls.append(ul.find('a').attrs['href'])
        ls_all.append(ls)
    return ls_all

    # li_list1 = soup.find_all('li', {'class': 'page_list_title'})
    # li_list2 = soup.find_all('li', {'class': 'page_list_date'})
    # all_ls = []
    # # ls = []
    # for lis in li_list1:
    #     ls = []
    #     li_list2[0].append('li'.string)
    #     tra = lis.find('a')
    #     ls.append(tra.attrs['title'])
    #     all_ls.append(ls)
    #     ls = []
    #
    #     all_ls.append(ls)
    # return all_ls


def wroitedata(ls):
    with open(r'温州职业技术学院.csv', 'w') as f:
        for line in ls:
            f.write(','.join(line) + '\n')


def main():
    url = 'https://www.wzvtc.cn/list/21.html'
    html = getHtml(url)
    ls = getHtmlData(html)
    wroitedata(ls)


main()
